<?php

namespace App\Http\Middleware;

use App\Enums\HttpType;
use Closure;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use App\Models\Admin;
use Illuminate\Http\Response;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class AdminMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param Request $request
     * @param Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return Response|RedirectResponse|JsonResponse|BinaryFileResponse
     */
    public function handle(Request $request, Closure $next): Response|RedirectResponse|JsonResponse|BinaryFileResponse
    {
        // @phpstan-ignore-next-line
        if (!(auth()->user() instanceof Admin)) {
            $result = [
                'code' => HttpType::TOKEN_INVALID->value,
                'data' => null,
                'msg' => '需要登录',
                'type' => 'error',
            ];
            return response()->json($result);
        }
        // @phpstan-ignore-next-line
        return $next($request);
    }
}
